package com.share.common.utils;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

public class ParamUtil {
    private static final String CHARSET_UTF_8 = "UTF-8";

    /**
     * eg :    a=2&b=4 或者  ?a=5&n=4
     * 获取URL中的参数, 在spring中可能返回的是数组
     */
    public static Map<String, String[]> kvs2map(String kvStr) {
        return kvToMap(kvStr, String[].class);
    }

    /**
     * eg :    a=2&b=4 或者  ?a=5&n=4
     * 获取URL中的参数
     */
    public static Map<String, String> kvToMap(String kvStr) {
        return kvToMap(kvStr, String.class);
    }

    private static <T> Map<String, T> kvToMap(String kvStr, Class<T> tClass) {
        Map<String, T> map = new HashMap<>();
        if (StringUtils.isEmpty(kvStr)) {
            return map;
        }

        //判断是否存在?
        String kvStrParam = kvStr;
        int i = kvStr.indexOf("?");
        if (i >= 0) {
            kvStrParam = kvStr.substring(i + 1);
        }

        String[] params = kvStrParam.split("&");
        for (String param : params) {
            String[] kv = param.split("=");
            if (ArrayUtils.isNotEmpty(kv)) {
                if (kv.length == 1) {
                    if (StringUtils.isEmpty(kv[0])) {
                        continue;
                    }
                    map.put(kv[0], null);
                } else if (kv.length == 2) {
                    if (StringUtils.isEmpty(kv[0])) {
                        continue;
                    }

                    try {
                        Object value = null;
                        String tmpValue = kv[1];
                        if (tClass.equals(String[].class)) {
                            value = tmpValue.split(",");
                        } else {
                            value = tmpValue;
                        }
                        map.put(kv[0], (T) value);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return map;
    }

    public static String mapToKv(Map<String, Object> params) {
        return mapToKv(params, true, true);
    }

    public static String mapToKv(Map<String, Object> params, boolean ignoreNull, boolean keyOrderAsc) {
        StringBuffer sb = new StringBuffer();

        if (!CollectionUtils.isEmpty(params)) {
            TreeSet<String> ts = new TreeSet();
            if (!keyOrderAsc) {
                ts = new TreeSet(Collections.reverseOrder());
            }
            ts.addAll(params.keySet());

            for (String key : ts) {
                Object tmpValue = params.get(key);
                String value = (tmpValue == null) ? "" : tmpValue.toString();
                if (ignoreNull && StringUtils.isEmpty(value)) {//判断时候忽略空值
                    continue;
                }
                sb.append("&" + key + "=" + value);
            }
            if (sb.length() > 1) {
                sb.deleteCharAt(0);/*把第一个字符删除*/
            }
        }
        return sb.toString();
    }
}
